package com.auoem.radio.common.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.ViewConfiguration;

import com.auctus.am.framework.utils.LogM;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * UI配置
 *
 * @author 李小�?
 */
public class ScreenUtils {
    private static final String TAG = "ScreenUtils";
    /**
     * 屏幕�?
     */
    public static int screenWidth;
    /**
     * 屏幕�?
     */
    public static int screenHeight;
    /**
     * 标题栏的高度
     */
    public static int screenTitleHeight = 0;

    /**
     * 分辨密度缩放大小
     */
    public static float Density = 1.0f;

    /**
     * 初始化屏幕的宽度,高度以及密度
     *
     * @param context
     */
    public static void init(Context context) {
        if (screenWidth == 0 || screenHeight == 0) {
            DisplayMetrics viewMEtrics = context.getResources()
                    .getDisplayMetrics();
            screenWidth = viewMEtrics.widthPixels;
            screenHeight = viewMEtrics.heightPixels;
            Density = viewMEtrics.density;
        }

        screenTitleHeight = ScreenUtils.getWindowStateSize(context);
    }

    /**
     * 获取通知栏高�?
     *
     * @param context
     * @return
     */
    private static int getWindowStateSize(Context context) {
        if (screenTitleHeight > 0)
            return screenTitleHeight;
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int sbar = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            sbar = context.getResources().getDimensionPixelSize(
                    Integer.parseInt(field.get(obj).toString()));
        } catch (Exception e1) {
            sbar = 0;
        }
        if (sbar <= 0) {
            @SuppressWarnings("deprecation")
            Drawable dra = context.getResources().getDrawable(
                    android.R.drawable.stat_sys_phone_call);
            sbar = dra.getIntrinsicHeight();
        }
        screenTitleHeight = sbar;
        return screenTitleHeight;
    }

    /**
     * 根据手机的分辨率�? dp 的单�? 转成�? px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率�? dp 的单�? 转成�? px(像素)
     */
    public static int dip2px(float dpValue) {
        // final float scale =
        // context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * Density + 0.5f);
    }

    /**
     * 根据手机的分辨率�? px(像素) 的单�? 转成�? dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率�? px(像素) 的单�? 转成�? dp
     */
    public static int px2dip(float pxValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static void getScreenSize(Activity activity, Point size) {
        if (null == activity) return;

        try {
            Display display = activity.getWindowManager().getDefaultDisplay();
            display.getSize(size);
        } catch (Exception e) {
            LogM.d(TAG, "-- get display err-" + e);
        }
    }

    // /**
    // * 返回PopupWindow显示的宽�?
    // * @param minSize
    // * @return
    // */
    // public static int getPopupwondowWidth()
    // {
    // if(ViewConfig.screenWidth >= 1080){
    // return 480;
    // }else if(ViewConfig.screenWidth >= 720){
    // return 300;
    // }else if(ViewConfig.screenWidth >= 480){
    // return 240;
    // }
    // return 250;
    // }

    /**
     * 返回缩放后的大小�?
     */
    public static int GetScreenScaleSize(int size) {
        if (Density == 1) {
            return size;
        }
        return (int) (size * Density + 0.2f);
    }

    //获取虚拟按键的高度
    public static int getNavigationBarHeight(Context context) {
        int result = 0;
        if (hasNavBar(context)) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    /**
     * 检查是否存在虚拟按键栏
     *
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static boolean hasNavBar(Context context) {
        Resources res = context.getResources();//读取系统资源函数
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");//获取资源id
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    /**
     * 判断虚拟按键栏是否重写
     *
     * @return
     */
    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
            }
        }
        return sNavBarOverride;
    }
}

